package com.haojiang.mh.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.haojiang.mh.R;
import com.haojiang.mh.utils.ImageUtil;
import com.haojiang.mh.utils.SizeUtil;

/**
 * Created by ZapFive on 2019-05-16.
 * <p>
 * wuzhuang@mirahome.me
 */
public class CustomToggle extends View implements View.OnClickListener {
    private Bitmap toggle_on;//打开
    private Bitmap toggle_off;//关闭
    private Bitmap bgBitmap;//默认
    private int viewW, viewH;
    private int defaultWidth, defaultHeight; //默认宽高
    private boolean checked;
    private TextPaint textPaint;
    private int text_color_on, text_color_off;
    private String text;
    private String text_on = "", text_off = "";
    private int text_size;
    private Matrix matrix = new Matrix();
    private OnCheckedChangeListener listener;
    private boolean isCanClick;

    public CustomToggle(Context context) {
        this(context, null);
    }

    public CustomToggle(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomToggle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.setOnClickListener(this);

        obtainStyledAttributes(context, attrs, defStyleAttr);

        initView();
    }

    private void obtainStyledAttributes(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomToggle, defStyleAttr, 0);
        int on = a.getResourceId(R.styleable.CustomToggle_toggle_on, 0);
        int off = a.getResourceId(R.styleable.CustomToggle_toggle_off, 0);
        toggle_on = ImageUtil.getBitmapFromDrawable(context, on == 0 ? R.drawable.ic_toggle_select : on);
        toggle_off = ImageUtil.getBitmapFromDrawable(context, off == 0 ? R.drawable.ic_toggle_normal : off);
        text_on = a.getString(R.styleable.CustomToggle_text_on);
        text_off = a.getString(R.styleable.CustomToggle_text_off);
        text_color_on = a.getColor(R.styleable.CustomToggle_text_color_on, Color.WHITE);
        text_color_off = a.getColor(R.styleable.CustomToggle_text_color_off, Color.WHITE);
        text_size = a.getDimensionPixelSize(R.styleable.CustomToggle_text_size, SizeUtil.sp2px(context, 16));
        checked = a.getBoolean(R.styleable.CustomToggle_default_checked, false);
        isCanClick = a.getBoolean(R.styleable.CustomToggle_default_click, true);

        a.recycle();
    }

    private void initView() {
        bgBitmap = toggle_off;

        defaultWidth = bgBitmap.getWidth();
        defaultHeight = bgBitmap.getHeight();

        if (!isDrawText()) return;
        text = text_off;
        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextSize(text_size);
        textPaint.setTextAlign(Paint.Align.CENTER);
    }

    private boolean isDrawText() {
        return !TextUtils.isEmpty(text_on) && !TextUtils.isEmpty(text_off);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        bgBitmap = checked ? toggle_on : toggle_off;
        matrix.reset();
        float scale = (float) viewW / bgBitmap.getWidth();
        matrix.postScale(scale, scale);
        canvas.drawBitmap(bgBitmap, matrix, null);

        if (!isDrawText()) return;
        textPaint.setColor(checked ? text_color_on : text_color_off);
        text = checked ? text_on : text_off;
        if (checked) {
            canvas.drawText(text, viewW * 0.35f, SizeUtil.getBaseLine(textPaint, viewH, viewH), textPaint);
        } else {
            canvas.drawText(text, viewW * 0.65f, SizeUtil.getBaseLine(textPaint, viewH, viewH), textPaint);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewW = w;
        viewH = h;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredLength(widthMeasureSpec, true),
                getMeasuredLength(heightMeasureSpec, false));
    }

    /**
     * 计算当前view的宽和高
     *
     * @param length
     * @param isWidth
     * @return
     */
    private int getMeasuredLength(int length, boolean isWidth) {
        int specMode = MeasureSpec.getMode(length);
        int specSize = MeasureSpec.getSize(length);
        int size;
        int padding = isWidth ? getPaddingLeft() + getPaddingRight() : getPaddingTop() + getPaddingBottom();
        if (specMode == MeasureSpec.EXACTLY) {
            size = specSize;
        } else if (specMode == MeasureSpec.UNSPECIFIED) {
            size = isWidth ? defaultWidth : defaultHeight;
        } else {
            size = isWidth ? padding + bgBitmap.getWidth() : padding + bgBitmap.getHeight();
            if (specMode == MeasureSpec.AT_MOST) {
                size = Math.min(size, specSize);
            }
        }

        return size;
    }

    /**
     * 添加监听
     *
     * @param listener
     */
    public void setOnCheckedChange(OnCheckedChangeListener listener) {
        this.listener = listener;
    }

    /**
     * 获取状态
     *
     * @return 当前状态
     */
    public boolean isChecked() {
        return checked;
    }

    /**
     * 设定状态
     *
     * @param checked 设定状态
     */
    public void setChecked(boolean checked) {
        this.checked = checked;
        invalidate();
    }

    public boolean isCanClick() {
        return isCanClick;
    }

    public void setCanClick(boolean canClick) {
        isCanClick = canClick;
    }

    @Override
    public void onClick(View v) {
        if (listener == null) return;
        listener.onCheckedChanged(this, !checked, isCanClick);
    }

    public interface OnCheckedChangeListener {
        /**
         * 当状态改变时调用
         *
         * @param toggle     view
         * @param isChecked  新状态
         * @param isCanClick 是否可以点击
         */
        void onCheckedChanged(CustomToggle toggle, boolean isChecked, boolean isCanClick);
    }
}